package com.atguigu.tingshu.search.service.impl;

import cn.hutool.core.lang.Assert;
import com.atguigu.tingshu.album.AlbumFeignClient;
import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.execption.GuiguException;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.model.album.BaseCategoryView;
import com.atguigu.tingshu.search.service.ItemService;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.album.AlbumStatVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
@SuppressWarnings({"all"})
public class ItemServiceImpl implements ItemService {

    @Autowired
    private UserFeignClient userFeignClient;

    @Autowired
    private AlbumFeignClient albumFeignClient;

    @Autowired
    @Qualifier("threadPoolTaskExecutor")
    private Executor executor;

    @Autowired
    private RedissonClient redissonClient;


    @Override
    public Map<String, Object> getItemInfo(Long albumId) {

        RBloomFilter<Long> bloomFilter = redissonClient.getBloomFilter(RedisConstant.ALBUM_BLOOM_FILTER);
        if (!bloomFilter.contains(albumId)) {
            throw new GuiguException(500, "访问专辑不存在");
        }

        Map<String, Object> map = new ConcurrentHashMap<>();
        //1. 获取专辑信息
        CompletableFuture<AlbumInfo> albumInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
            Assert.notNull(albumInfo, "专辑信息不存在！");
            map.put("albumInfo", albumInfo);
            return albumInfo;
        }, executor);


        //2. 获取用户信息
        CompletableFuture<Void> announcerCompletableFuture =
                albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
                    UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(albumInfo.getUserId()).getData();
                    Assert.notNull(userInfoVo, "用户信息不存在！");
                    map.put("announcer", userInfoVo);
                }, executor);


        //4. 获取专辑统计信息
        CompletableFuture<Void> statCompletableFuture = CompletableFuture.runAsync(() -> {
            AlbumStatVo albumStatVo = albumFeignClient.getAlbumStatVo(albumId).getData();
            Assert.notNull(albumStatVo, "专辑统计信息不存在！");
            map.put("albumStatVo", albumStatVo);
        }, executor);


        //3. 获取专辑分类信息
        CompletableFuture<Void> categoryViewCompletableFuture = albumInfoCompletableFuture.thenAcceptAsync(albumInfo -> {
            BaseCategoryView baseCategoryView =
                    albumFeignClient.getCategoryView(albumInfo.getCategory3Id()).getData();
            Assert.notNull(baseCategoryView, "专辑分类信息不存在！");
            map.put("baseCategoryView", baseCategoryView);
        }, executor);
        //6.等待所有任务执行完成
        CompletableFuture.allOf(
                statCompletableFuture,
                albumInfoCompletableFuture,
                categoryViewCompletableFuture,
                announcerCompletableFuture
        ).orTimeout(1, TimeUnit.SECONDS).join();

        //7.响应map对象
        return map;
    }
}
